package com.eostoken.eos_wallet.ui.fragment;

import android.view.View;
import android.widget.EditText;

import androidx.appcompat.widget.AppCompatCheckBox;
import androidx.appcompat.widget.AppCompatEditText;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.blankj.utilcode.util.FragmentUtils;
import com.blankj.utilcode.util.ObjectUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.eostoken.eos_wallet.R;
import com.eostoken.eos_wallet.base.BaseFragment;
import com.eostoken.eos_wallet.db.MgpWallet;
import com.eostoken.eos_wallet.utils.WalletUtils;
import com.fredy.mvp.BasePresenter;
import com.fredy.mvp.BaseView;
import com.fredy.mvp.Model;
import com.google.android.material.textfield.TextInputEditText;
import com.qmuiteam.qmui.widget.QMUITopBarLayout;
import com.qmuiteam.qmui.widget.roundwidget.QMUIRoundButton;

import butterknife.BindView;
import butterknife.OnClick;

public class ChangePasswordFragment extends BaseFragment {
    @BindView(R.id.topbar)
    QMUITopBarLayout topbar;
    @BindView(R.id.createWalletExplainTv)
    AppCompatTextView createWalletExplainTv;
    @BindView(R.id.accountNameTv)
    AppCompatTextView accountNameTv;
    @BindView(R.id.accountNameEt)
    AppCompatEditText accountNameEt;
    @BindView(R.id.stateTv)
    AppCompatTextView stateTv;
    @BindView(R.id.line1)
    View line1;
    @BindView(R.id.passwordTv)
    AppCompatTextView passwordTv;
    @BindView(R.id.passwordEt)
    AppCompatEditText passwordEt;
    @BindView(R.id.line2)
    View line2;
    @BindView(R.id.confirmPasswordTv)
    AppCompatTextView confirmPasswordTv;
    @BindView(R.id.confirmPasswordEt)
    AppCompatEditText confirmPasswordEt;
    @BindView(R.id.line3)
    View line3;
    @BindView(R.id.promptMessageTv)
    AppCompatTextView promptMessageTv;
    @BindView(R.id.promptMessageEt)
    AppCompatEditText promptMessageEt;
    @BindView(R.id.line4)
    View line4;
    @BindView(R.id.clauseCheckBox)
    AppCompatCheckBox clauseCheckBox;
    @BindView(R.id.layout)
    ConstraintLayout layout;
    @BindView(R.id.createWalletBtn)
    QMUIRoundButton createWalletBtn;

    private MgpWallet wallet;

    @Override
    protected int getContentLayout() {
        return R.layout.fragment_create_account;
    }

    @Override
    protected void initData() {
        wallet = WalletUtils.getCurrentWallet(getActivity());
    }

    @Override
    protected void initView(View contentView) {
        topbar.setTitle(StringUtils.getString(R.string.str_change_password));
        topbar.addLeftBackImageButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (getFragmentManager().getBackStackEntryCount() < 1) {
                    finish();
                } else {
                    FragmentUtils.pop(getFragmentManager());
                }
            }
        });

        clauseCheckBox.setVisibility(View.GONE);
        unEditableEditText(accountNameEt);
        accountNameEt.setText(wallet.getCurrentAccountName());
        createWalletBtn.setText(StringUtils.getString(R.string.str_ok));
    }

    public void unEditableEditText(EditText editText) {
        editText.setEnabled(false);//去掉点击时编辑框下面横线:
        editText.setFocusable(false);//不可编辑
        editText.setFocusableInTouchMode(false);//不可编辑
    }

    @Override
    public Model createModel() {
        return null;
    }

    @Override
    public BaseView createView() {
        return null;
    }

    @Override
    public BasePresenter createPresenter() {
        return null;
    }

    @Override
    public void finishRefresh() {

    }

    @OnClick(R.id.createWalletBtn)
    public void onViewClicked() {
        String password = passwordEt.getText().toString().trim();
        String confirmPassword = confirmPasswordEt.getText().toString().trim();
        String hint = promptMessageEt.getText().toString().trim();
        if (ObjectUtils.isEmpty(password)) {
            ToastUtils.showLong(R.string.str_input_password);
            return;
        }
        if (password.getBytes().length < 6) {
            ToastUtils.showLong(R.string.str_hint_password);
            return;
        }
        if (!ObjectUtils.equals(password, confirmPassword)) {
            ToastUtils.showLong(R.string.str_inconformity_password);
            return;
        }
        wallet.setWalletPassword(password);
        if (!ObjectUtils.isEmpty(hint)) {
            wallet.setHint(hint);
        }
        WalletUtils.updateWallet(getActivity(), wallet);
        ToastUtils.showLong(R.string.str_modify_success);
        finish();
    }
}
